package leetcode.code2498;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int maxJump(int[] stones) {
		int ans = 0;
		int l = 1, r = Arrays.stream(stones).max().getAsInt();
		int t = 1;
		int[] tt = new int[stones.length];
		while (l <= r) {
			int m = ((r - l) >> 1) + l;
			boolean can = true;
			int last = 0, max = -1;
			for (int i = 1; i < stones.length && can; ) {
				int cur = stones[i];
				if (cur - stones[last] <= m) {
					max = i;
					i++;
				} else {
					if (max == -1) {
						can = false;
					} else {
						tt[max] = t;
						last = max;
						max = -1;
					}
				}
			}
			last = stones.length - 1;
			max = -1;
			for (int i = stones.length - 2; can && i >= 0; ) {
				if (tt[i] == t) {
					i--;
					continue;
				}
				int cur = stones[i];
				if (stones[last] - cur <= m) {
					max = i;
					i--;
				} else {
					if (max == -1) {
						can = false;
					} else {
						last = max;
						max = -1;
					}
				}
			}
			if (can) {
				ans = m;
				r = m - 1;
			} else {
				l = m + 1;
			}
			t++;
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(20, this.maxJump(H.str2array3("[0,5,12,25,28,35]")));
		H.compare(5, this.maxJump(H.str2array3("[0,2,5,6,7]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
